package com.neusoft.dibuy;

public class J0809 {
    public static void main(String[] args) {
        int[] is = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        int lidnex = 0;
        int rindex = is.length - 1;
        int left = 0;
        int right = is.length - 1;
        int maxv = (right - left) * fmin(is, left, right);
        while (left < right) {
            if (is[left] < is[right]) {
//                向右移动left
                int lv = is[left];
                while (is[++left] < lv) ;
            } else {
//                向左移动right；
                int lv = is[right];
                while (is[--right] < lv) ;

            }
            if ((right - left) * fmin(is, left, right) > maxv) {
                maxv = (right - left) * fmin(is, left, right);
                lidnex = left;
                rindex = right;
            }

        }
        System.out.println(maxv);
        System.out.println(lidnex);
        System.out.println(rindex);

    }

    private static int fmin(int[] is, int left, int right) {
        return is[left] > is[right] ? is[right] : is[left];
    }


    public static void main1(String[] args) {
        int[] is = {1, 8, 6, 2, 5, 4, 8, 3, 7};
//        假定起始状态的最大值
        int idnexi = 0;
        int indexj = 1;
        int maxv = 1;
        for (int i = 0; i < is.length - 1; i++) {
            for (int j = i + 1; j < is.length; j++) {
                int vi = is[i];
                int vj = is[j];
                int minv = vi > vj ? vj : vi;
                int v = minv * (j - i);
                if (v > maxv) {
                    maxv = v;
                    idnexi = i;
                    indexj = j;
                }
            }
        }
        System.out.println(maxv);
        System.out.println(idnexi);
        System.out.println(indexj);
    }
}
